#include<iostream>
#include<algorithm>
#include<tuple>
using namespace std;


struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
 
class Solution {
    int helper(TreeNode* root,TreeNode*& prev) {
        int res = INT32_MAX;
        if (root == NULL) return res;
        res = min(helper(root->left, prev), res);
        if (prev != NULL) 
            res = min(res, abs(root->val - prev->val));
        prev = root;
        return min(res, helper(root->right, prev));
    }


    //void helper(TreeNode* root) {
    //    int res = INT16_MAX;
    //    auto left = root->left;
    //    auto right = root->right;
    //    if (left != NULL) {
    //        helper(left);
    //        res = left->val;
    //        res = min(left->val, abs(left->right->val - root->val));
    //        root->left = left->left;
    //    }
    //    else
    //        root->left = new TreeNode(root->val);

    //    if (right != NULL) {
    //        helper(right);
    //        res = min(res, right->val);
    //        res = min(res, abs(right->left->val - root->val));
    //        root->right = right->right;
    //    }else
    //        root->right = new TreeNode(root->val);
    //    root->val = res;
    //}
public:
    int getMinimumDifference(TreeNode* root){
        TreeNode* node = NULL;
        return helper(root, node);
    }
};